package com.leyou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.entity.ClientInfo;
import com.leyou.mapper.ClientMapper;
import com.leyou.myException.LyException;
import com.leyou.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, ClientInfo> implements ClientService {
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${ly.jwt.key}")
    private String key;
    @Override
    public String getSecretKey(String clientId, String secret) {
        ClientInfo clientInfo = query().eq("client_id", clientId).one();
        if (clientInfo==null){
            throw new LyException(400,"客户端信息有误,clientId"+clientId+"不存在");
        }
        //第一个参数是加密前的，第二个是加密后的
        boolean matches = passwordEncoder.matches(secret,clientInfo.getSecret());
        if (matches){
            return key;
        }else {
            throw new LyException(400,"错误的客户端,secret错误");
        }
    }
}